package com.spzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.user.domain.UserCollect;
import com.spzx.user.mapper.UserCollectMapper;
import com.spzx.user.service.IUserCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户收藏表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-11-18
 */
@Service
public class UserCollectServiceImpl extends ServiceImpl<UserCollectMapper, UserCollect> implements IUserCollectService {
    @Override
    public void doCollect(Long skuId) {
//        LambdaQueryWrapper<UserCollect> queryWrapper = new LambdaQueryWrapper<UserCollect>()
//                .eq(UserCollect::getSkuId, skuId);
//        UserCollect userCollectOne = baseMapper.selectOne(queryWrapper);
//        if (userCollectOne == null){
            UserCollect userCollect = new UserCollect();
            userCollect.setUserId(SecurityUtils.getUserId());
            userCollect.setSkuId(skuId);
            userCollect.setCreateBy(SecurityUtils.getUsername());
            baseMapper.insert(userCollect);
//        }else {
//            userCollectOne.setDelFlag("0");
//            baseMapper.updateById(userCollectOne);
//        }
    }

    @Override
    public void cancelCollect(Long skuId) {
        LambdaQueryWrapper<UserCollect> queryWrapper = new LambdaQueryWrapper<UserCollect>()
                .eq(UserCollect::getSkuId, skuId);
        UserCollect userCollectOne = baseMapper.selectOne(queryWrapper);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public Boolean isCollect(Long skuId) {
        LambdaQueryWrapper<UserCollect> queryWrapper = new LambdaQueryWrapper<UserCollect>()
                .eq(UserCollect::getSkuId, skuId);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0){
            return true;
        }
        return false;
    }
}
